import PropTypes from 'prop-types'
import CommonModalForm from './common/CommonModalForm'
import { Button, message } from 'antd'
import { useState } from 'react'
import { useAsNewVersion } from "../apis/model"

const ModelerSetNewVersion = ({
  modelId,
  versionId,
  onSuccess
}) => {

  const [newVersionFormVisible, setNewVersionFormVisible] = useState(false)

  const handleSubmitNewVersion = async (values) => {
    try {
      await useAsNewVersion({
        modelId,
        versionId,
        action: 'useAsNewVersion',
        ...values
      })
      message.success('请求成功')
      setNewVersionFormVisible(false)
      onSuccess()
    } catch (error) {
      message.error('请求失败')
    }
  }

  return <>
    <CommonModalForm
      title="作为新版本使用"
      visible={newVersionFormVisible}
      onCancel={() => { setNewVersionFormVisible(false) }}
      onSubmit={handleSubmitNewVersion}
      formItems={[
        {
          name: 'comment',
          label: '版本说明',
          type: 'textarea',
          required: true
        }
      ]}
    ></CommonModalForm>
    <Button
      type="primary"
      onClick={() => { setNewVersionFormVisible(true) }}
    >作为新版本使用</Button>
  </>
}

ModelerSetNewVersion.propTypes = {
  modelId: PropTypes.string,
  versionId: PropTypes.string,
  onSuccess: PropTypes.func
}

export default ModelerSetNewVersion